<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//部门管理模块
class Productsbrands extends Common
{
	public function huishou($page=1,$list=10){

		$where=["is_actived"=>0,"is_deleted"=>0];
		//数据总量
		$sum=Db::table("pro_brands")->where($where)->count();
		
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;


        $pro_bs=Db::table("pro_brands")->alias("pb")
        ->join("users uc","pb.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","pb.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where("pb.is_actived=0 and pb.is_deleted=0")
        ->order("pb.id")
        ->limit($start,$list)
        ->field("pb.id,pb.brand_name,pb.profit,ec.em_name as cname,pb.created_on,em.em_name as mname,pb.modifiled_on")
        ->select();
        //日期处理
        for($i=0;$i<count($pro_bs);$i++){
        	$pro_bs[$i]["created_on"]=date("Y-m-d H:i",strtotime($pro_bs[$i]["created_on"]));
        	if($pro_bs[$i]["modifiled_on"])
        		$pro_bs[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($pro_bs[$i]["modifiled_on"]));
        	$pro_bs[$i]["profit"]=number_format($pro_bs[$i]["profit"],2);
        }

		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("pro_bs",$pro_bs);
		return $this->fetch('huishou');
	}
	public function brands($page=1,$list=10){

		$where=["is_actived"=>1,"is_deleted"=>0];
		//数据总量
		$sum=Db::table("pro_brands")->where($where)->count();
		
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;


        $pro_bs=Db::table("pro_brands")->alias("pb")
        ->join("users uc","pb.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","pb.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where("pb.is_actived=1 and pb.is_deleted=0")
        ->order("pb.id")
        ->limit($start,$list)
        ->field("pb.id,pb.brand_name,pb.profit,ec.em_name as cname,pb.created_on,em.em_name as mname,pb.modifiled_on")
        ->select();
        //日期处理
        for($i=0;$i<count($pro_bs);$i++){
        	$pro_bs[$i]["created_on"]=date("Y-m-d H:i",strtotime($pro_bs[$i]["created_on"]));
        	if($pro_bs[$i]["modifiled_on"])
        		$pro_bs[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($pro_bs[$i]["modifiled_on"]));
        	$pro_bs[$i]["profit"]=number_format($pro_bs[$i]["profit"],2);
        }

		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("pro_bs",$pro_bs);
		return $this->fetch('brands');
	}
	//新增或修改品牌
	public function new_brand(){
		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        $id=$datas["id"];$brands_name=trim($datas["brands_name"]);$profit=trim($datas["profit"]);
		if(!trim($brands_name))
			return 2;//产品名空
		if(!trim($profit))
			return 3;//费率空
		if($id){
			//修改
			$iscunzai=Db::table("pro_brands")->where("brand_name='$brands_name' and is_deleted=0 and id<>$id")->count();
			if($iscunzai)
				return 4;//重名
			$data["id"]=$id;
			$data["brand_name"]=$brands_name;
			$data["profit"]=$profit;
			$data["m_users_id"]=Session::get("login_id");
			$data["modifiled_on"]=date("Y-m-d H:i:s");
			Db::startTrans(); //开启事物
			 try{
                $res1=Db::table("pro_brands")->update($data);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1){
             	Db::commit();
             	return 1;
             }else{
             	Db::rollBack();
                  return 5;//失败
             }
		}else{
			//新增
			
			$iscunzai=Db::table("pro_brands")->where("brand_name='{$brands_name}' and is_deleted=0")->count();
			if($iscunzai)
				return 4;//重名

			$data["brand_name"]=$brands_name;
			$data["profit"]=$profit;
			$data["c_users_id"]=Session::get("login_id");
			$data["created_on"]=date("Y-m-d H:i:s");
			Db::startTrans(); //开启事物
			 try{
                $res1=Db::table("pro_brands")->insert($data);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1){
             	Db::commit();
             	return 1;
             }else{
             	Db::rollBack();
                  return 5;//失败
             }
		}

	}
	//获取品牌详情
	public function getbrand(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //$data["bid"]=2;
		$where=["id"=>$data["bid"]];
		$arr=Db::table("pro_brands")->where($where)->find();
		return json_encode($arr);
	}
	//删除品牌
	public function dropbran(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据

        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["bid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_brands")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复品牌
	public function huifubran(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据

        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["bid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_brands")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	public function getbrandlist(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据

        $result=[];
        $where=["is_actived"=>1,"is_deleted"=>0];
       	$res=Db::table("pro_brands")->where($where)->select();
       	 for($i=0;$i<count($res);$i++){
                $result[$i]["id"]=$res[$i]["id"];
               
                $result[$i]["name"]=$res[$i]["brand_name"];
                if($res[$i]["id"]==$data["bid"]){
                    $result[$i]["checked"]=true;
                }
               
                
           }
           
         
           return json_encode($result);
	}
    //产品大品牌
     public function bigbrands($page=1,$list=10){
        $request = Request::instance();

        
        $data=$request->post();
        $where="pb.is_deleted=0 ";
        //数据总量
        if($data){
            
            if(trim($data["big_name"]))
                $where.="and pb.big_name like '%{$data["big_name"]}%' ";
           
            if(trim($data["big_no"]))
                $where.="and pb.big_no like '%{$data["big_no"]}%' ";


        }else{
            $data=["big_name"=>"","big_no"=>""];
        }
        $sum=Db::table("pro_big_brands")->alias("pb")->where($where)->count();
        
        $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $pro_bs=Db::table("pro_big_brands")->alias("pb")
        ->join("users uc","pb.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","pb.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where($where)
        ->order("id")
        ->limit($start,$list)
        ->field("pb.id,pb.big_name,pb.big_no,pb.is_actived,ec.em_name as cname,pb.created_on,em.em_name as mname,pb.modifiled_on")
        ->select();
        //日期处理
        $bids=[];
        for($i=0;$i<count($pro_bs);$i++){
            $pro_bs[$i]["created_on"]=date("Y-m-d H:i",strtotime($pro_bs[$i]["created_on"]));
            if($pro_bs[$i]["modifiled_on"])
                $pro_bs[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($pro_bs[$i]["modifiled_on"]));
           
            $bids[]=$pro_bs[$i]["id"];
        }
        $str=implode(",", $bids);
        $barr=Db::table("pro_brands_group")->alias("bg")
        ->join("pro_brands b","bg.brand_id=b.id")
        ->where("bg.big_id in ($str) and b.is_deleted=0 and b.is_actived=1")
        ->field("b.brand_name,bg.big_id")
        ->select();
        //查询品牌
        //
        //
        $this->assign("data",$data);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("pro_bs",$pro_bs);
        $this->assign("barr",$barr);
        return $this->fetch('bigbrands');
    }

    //删除品牌
    public function dropbb(){
        $request = Request::instance();
        
        $data=$request->post();//接收数据

        //$data["m_users_id"]=Session::get("login_id");
            //$data["modifiled_on"]=date("Y-m-d H:i:s");
        $where=["id"=>$data["bid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
        $res=Db::table("pro_big_brands")->update($where);
        if($res)
            return 1;
        else
            return 2;
    }
    //恢复品牌
    public function huifubb(){
        $request = Request::instance();
        
        $data=$request->post();//接收数据

        //$data["m_users_id"]=Session::get("login_id");
            //$data["modifiled_on"]=date("Y-m-d H:i:s");
        $where=["id"=>$data["bid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
        $res=Db::table("pro_big_brands")->update($where);
        if($res)
            return 1;
        else
            return 2;
    }
    public function getbrandlist_duo(){
        $request = Request::instance();
        
        $data=$request->post();//接收数据
        if($data["rids"])
            $ridsarr=explode(",", $data["rids"]);
        else
            $ridsarr=[];
        $result=[];
        $where=["is_actived"=>1,"is_deleted"=>0];
        $res=Db::table("pro_brands")->where($where)->select();
         for($i=0;$i<count($res);$i++){
                $result[$i]["id"]=$res[$i]["id"];
               
                $result[$i]["name"]=$res[$i]["brand_name"];
                if(in_array($result[$i]["id"],$ridsarr)){
                    $result[$i]["checked"]=true;
                }
               
                
           }
           
         
           return json_encode($result);
    }
    public function getbigbrand(){
         $request = Request::instance();
        
        $data=$request->post();//接收数据
        $bid=$data["bid"];
        $bigbrands=Db::table("pro_big_brands")->where(["id"=>$bid])->find();
        $brands_arr=Db::table("pro_brands_group")->alias("bg")
        ->join("pro_brands b","bg.brand_id=b.id")
        ->where("bg.big_id=$bid and b.is_deleted=0 and b.is_actived=1")
        ->field("b.brand_name,bg.big_id,b.id")
        ->select();
        $data["big_brand"]= $bigbrands;
        $data["brands_arr"]=$brands_arr;
        return json_encode($data);
    }
    public function new_big_brand(){
        $request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
            return 0;
        }
        //id:$("#isupdate").val(),big_name:$("#big_name").val(),big_no:$("#big_no").val(),bids:bids
        $id=$datas["id"];$big_name=trim(htmlspecialchars($datas["big_name"]));$big_no=trim($datas["big_no"]);
        $bids=trim(htmlspecialchars($datas["bids"]));
        
        if(!$big_name)
            return 2;//系列名空
        if(!$bids)
            return 3;//品类空
      
        if($id){
            //修改
            $iscunzai=Db::table("pro_big_brands")->where("big_name='{$big_name}' and is_deleted=0 and id<>$id")->count();
            if($iscunzai)
                return 4;//重名
            $data["id"]=$id;
            $data["big_name"]=$big_name;
           
            $data["m_users_id"]=Session::get("login_id");
            $data["modifiled_on"]=date("Y-m-d H:i:s");
            Db::startTrans(); //开启事物
             try{
                $res1=Db::table("pro_big_brands")->update($data);
                Db::table("pro_brands_group")->where("big_id=$id")->delete();
                 $bidarr=explode(',',$bids);
                $biddata=[];//echo 1;exit;
                for($i=0;$i<count($bidarr);$i++){
                    $biddata[$i]["big_id"]=$id;
                    $biddata[$i]["brand_id"]=$bidarr[$i];
                }
                $res2=Db::table("pro_brands_group")->insertAll($biddata);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1&$res2){
                Db::commit();
                return 1;
             }else{
                Db::rollBack();
                  return 5;//失败
             }
        }else{
           if(!$big_no){
                //没有编号自动生成
               
                $big_no="BGB".rand(0,100).time().Session::get("login_id");
            }

            $iscunzai=Db::table("pro_big_brands")->where("big_name='{$big_name}'  and is_deleted=0")->count();
            if($iscunzai)
                return 4;//重名
            $iscunzain=Db::table("pro_big_brands")->where("big_no='{$big_no}'  and is_deleted=0")->count();
            if($iscunzain)
                return 7;//重号
            //创建
             //exit;
            $data["big_name"]=$big_name;
            $data["big_no"]=$big_no;
            $data["c_users_id"]=Session::get("login_id");
            $data["created_on"]=date("Y-m-d H:i:s");
            

            Db::startTrans(); //开启事物
             try{
                $res1=Db::table("pro_big_brands")->insertGetId($data);
                $bidarr=explode(',',$bids);
                $biddata=[];//echo 1;exit;
                for($i=0;$i<count($bidarr);$i++){
                    $biddata[$i]["big_id"]=$res1;
                    $biddata[$i]["brand_id"]=$bidarr[$i];
                }
                $res2=Db::table("pro_brands_group")->insertAll($biddata);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 51;//失败
             }
             if($res1&&$res2){
                Db::commit();
                return 1;
             }else{
                Db::rollBack();
                  return 52;//失败
             }

        }
    }
}